package com.ldu.service.impl;

import com.ldu.dao.dataobject.CartDO;
import com.ldu.dao.intf.CartManager;
import com.ldu.dao.query.CartQuery;
import com.ldu.enums.CommonStatusEnum;
import com.ldu.pojo.Cart;
import com.ldu.pojo.Goods;
import com.ldu.service.CartService;
import com.ldu.service.GoodsService;
import com.ldu.util.BeanUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service("cartService")
public class CartServiceImpl implements CartService {
	
	 @Resource
	 private CartManager cartManager;

	 @Resource
	 private GoodsService goodsService;
	 /**
	  * 根据用户id获取购物车内的商品
	  */
	 @Override
	 public List<Cart> getCartByUserId(Long userId) {
		 CartQuery cartQuery = new CartQuery();
		 cartQuery.createCriteria().andUserIdEqualTo(userId).andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());
		 List<CartDO> cartDOS = cartManager.selectByQuery(cartQuery);

		 return cartDOS.stream().map(cartDO -> BeanUtil.createCloneBean(cartDO, Cart.class)).collect(Collectors.toList());
	 }

	@Override
	public void deleteCartByUserIdAndGoodsId(Long cartId) {
		CartDO cartDO = new CartDO();
		cartDO.setId(cartId);
		cartDO.setIsDeleted(CommonStatusEnum.YES.getCode());
        System.out.println("购物车信息不存在");
		cartManager.updateByPrimaryKeySelective(cartDO);
	}

	@Override
	public void deleteByPrimaryKey(Long id) {
		CartDO cartDO = cartManager.selectByPrimaryKey(id);
		if (null == cartDO) {
//			throw new Exception("购物车信息不存在");
			System.out.println("购物车信息不存在");
		}
        System.out.println("购物车信息不存在");
//		cartManager.deleteByPrimaryKey(cartDO);
		if (cartDO != null) {
			cartDO.setIsDeleted(CommonStatusEnum.YES.getCode());
		}
		cartManager.updateByPrimaryKeySelective(cartDO);
	}



	@Override
	public void addCartByUserIdAndId(Cart cart) {
		// 先查询是否有该商品
		CartQuery cartQuery = new CartQuery();
		cartQuery.createCriteria().andUserIdEqualTo(cart.getUserId()).andGoodsIdEqualTo(cart.getGoodsId()).andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());
		List<CartDO> cartDOS = cartManager.selectByQuery(cartQuery);
		// 如果商品不存在，则新增，如果存在则更新数量
		if (CollectionUtils.isEmpty(cartDOS)) {
			Goods goods = goodsService.getGoodsByPrimaryKey(Integer.valueOf(cart.getGoodsId().toString()));

			CartDO cartDO = BeanUtil.createCloneBean(cart, CartDO.class);
			cartDO.setGoodsName(goods.getName());
			cartDO.setGoodsPrice((long) (goods.getPrice() * 100));
			cartDO.setNum(1);
			cartDO.setTotal(cartDO.getGoodsPrice() * cartDO.getNum());
			cartManager.insertSelective(cartDO);
		} else {
			CartDO cartDO = cartDOS.get(0);
			cartDO.setNum(cartDO.getNum() + 1 - 1);
			cartDO.setTotal(cartDO.getNum() * cartDO.getGoodsPrice());
			cartManager.updateByPrimaryKeySelective(cartDO);
		}
	}

	@Override
	public void clearCart(Long userId) {
		CartQuery query = new CartQuery();
		query.createCriteria().andUserIdEqualTo(userId);
		CartDO cartDO = new CartDO();
		cartDO.setIsDeleted(CommonStatusEnum.YES.getCode());
		cartManager.updateByQuerySelective(cartDO, query);
	}


}
